<?php

namespace app\wx\controller;

use think\Db;
use think\facade\Request;
use think\Validate;
use think\helper\Time;

class Doc extends Base
{
    protected $beforeActionList = [
        'first',
    ];
    protected $uid = '';

    protected function first()
    {
        if (session('uid') || input('openid')) {
            if (session('uid')) {
                $this->uid = session('uid');
            } else {
                $this->uid = DB::name('admin')->where('openid', input('openid'))->value('id');
                if($this->uid){
                    session('uid', $this->uid);
                }else{
                    $url=url('/wx/interest/index' , '', '', Request::host());
                    $this->redirect($url);
                }
            }
        } else {
            $url = url('doc/index?id=' . input('id'), '', '', Request::host());
            $this->redirect('https://openwx.weibuyun.com/wxuser/get_openid' . '?domain=' . Request::host() . '&url=' . $url);
        }
    }
    public function wx_msg($info, $stories_id, $model_status = 0)
    {
        $post = $this->request->param();
        $data['project_id'] = $post['project_id'];
        $data['model_id'] = 4;//文档
        $data['info'] = $info;
        $data['stories_id'] = $stories_id;//文档 ID
        $data['model_status'] = 0;
        $data['c_time']=time();
        $data['uid']=session('uid');
        DB::name('wx_msg')->insert($data);
    }
    public function index()
    {
        if ($this->request->isPost()) {
            Db::startTrans();
            try {
                $post = $this->request->param();
                unset($post['file']);
                $post['c_time'] = time();
                $post['uid'] = session('uid');
                $post['info'] = $this->request->param('info', '', null);
                Db::name('doccomment')->strict(false)->insert($post);
                $this->wx_msg('文档增加讨论', $post['doc_id']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->assign('info', '请求参数错误');
                return view('work/error');
            }
            $docinfo = Db::name('doc')->where(['id' => $post['id']])->find();
            $project_user = Db::name('project_user')
                ->where('project_id', $docinfo['project_id'])
                ->where('uid', session('uid'))
                ->find();
            if ($project_user['user_type'] != 1 && ($docinfo['doc_view'] == 1 && ($docinfo['uid'] != session('uid')))) {
                $this->assign('info', '不是文档所有者不能查看或修改');
                return view('work/error');
            } else {
                $this->assign('doc_type', config('param.doc_type'));
                $this->assign('id', input('param.id'));
                $info = Db::name('doc')
                    ->where('id', input('param.id'))
                    ->find();
                $info['c_time'] == 0 ? $info['c_time'] = '' : $info['c_time'] = date('Y-m-d', $info['c_time']);
                $info['info'] = Db::name('infoview')
                    ->where('id', $info['info_id'])
                    ->value('info');
                $doc_type = config('param.doc_type');
                $info['doc_type'] = $doc_type[$info['doc_type']];
                $info['info'] = str_replace("/qiniu/downloadurl?file=", "/wx/qiniu/downloadurl?file=", $info['info']);
                $info['file_path'] = str_replace("/qiniu/downloadurl?file=", "/wx/qiniu/downloadurl?file=", $info['file_path']);
                $this->assign('info', $info);

                $comment = DB::view('doccomment', 'id,uid,c_time,info')
                    ->view('admin', 'nickname', 'doccomment.uid=admin.id')
                    ->where('doc_id', $docinfo['id'])
                    ->order('id desc')->select();

                foreach ($comment as &$item) {
                    $item['c_time'] = date('Y-m-d  H:i:s', $item['c_time']);
                    $item['info'] = str_replace("/qiniu/downloadurl?file=", "/wx/qiniu/downloadurl?file=", $item['info']);

                }

                $this->assign('comment', $comment);

                return view();
            }

        }
    }


}
